package com.fp.m7.manager;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.fp.m7.api.manager.AccountManager;
import com.fp.m7.api.model.Account;
import com.fp.m7.dao.AccountDao;

public class AccountManagerImpl implements AccountManager {

	@Autowired
	private AccountDao accountDao;
	
	@Override
	public Account findById(Serializable id) {
		return accountDao.getById((Long) id);
	}

	@Override
	public Account update(Account account) {
		return accountDao.merge((com.fp.m7.model.Account) account);
	}

	@Override
	public void delete(Account account) {
		accountDao.remove((com.fp.m7.model.Account) account);
	}

	@Override
	public Account createNew(String username, String password, String nickname) {
		Account account = new com.fp.m7.model.Account();
		account.setUsername(username);
		account.setPassword(password);
		account.setNickname(nickname);
		return accountDao.persist((com.fp.m7.model.Account) account);
	}

	@Override
	public Account findByUsername(String username) {
		return accountDao.findByUsername(username);
	}

	@Override
	public Account findByNickname(String nickname) {
		return accountDao.findByNickname(nickname);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Account> findRandom(int count) {
		List found = accountDao.findRandom(count);
		return found;
	}
	
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

}
